package com.zusmart.base.activity.support;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.zusmart.base.activity.Attachable;

public abstract class AbstractAttachable<K, V> implements Attachable<K, V> {

	private Map<K, V> properties;

	@Override
	public V getProperty(K key) {
		if (null == key || null == this.properties) {
			return null;
		}
		return this.properties.get(key);
	}

	@Override
	public void setProperty(K key, V value) {
		if (null == key) {
			return;
		}
		if (null == this.properties) {
			this.properties = new HashMap<K, V>();
		}
		this.properties.put(key, value);
	}

	@Override
	public void delProperty(K key) {
		if (null == key || null == this.properties) {
			return;
		}
		this.properties.remove(key);
	}

	@Override
	public boolean hasProperty(K key) {
		if (null == key || null == this.properties) {
			return false;
		}
		return this.properties.containsKey(key);
	}

	@Override
	public Map<K, V> getProperties() {
		if (null == this.properties) {
			return Collections.emptyMap();
		} else {
			return Collections.unmodifiableMap(this.properties);
		}
	}

	@Override
	public void clearProperties() {
		if (null != this.properties) {
			this.properties.clear();
		}
	}

}